#include<iostream>
using namespace std;

struct edge
{
	int to, nxt;
}e[6005];

int head[6005];
int fa[6005];
int happy_ind[6005];
int dp[6005][2];

int ind = 0;

void add(int a, int b)
{
	e[++ind] = { b, head[a] };
	head[a] = ind;
	fa[b] = 1;
}

void output(int n)
{
	for (int i = 1; i <= n; i++)
	{
		cout << i << " : ";
		for (int j = head[i]; j; j = e[j].nxt)
		{
			int to = e[j].to;
			cout << to << " ";
		}
		cout << endl;
	}
}

void dfs(int root)
{
	dp[root][0] = 0;
	dp[root][1] = happy_ind[root];
	for (int i = head[root]; i; i = e[i].nxt)
	{
		int to = e[i].to;
		dfs(to);
		dp[root][0] += max(dp[to][0], dp[to][1]);
		dp[root][1] += dp[to][0];
	}
}

int main()
{
	int n;
	cin >> n;
	int step = n;
	for (int i = 1; i <= n; i++) cin >> happy_ind[i];
	while (--step)
	{
		int a, b;
		cin >> a >> b;
		add(b, a);
	}
	int root = 0;
	cin >> root >> root;
	//output(n);
	for (int i = 1; i <= n; i++)
	{
		if (fa[i]) continue;
		root = i;
		break;
	}
	dfs(root);
	cout << max(dp[root][0], dp[root][1]) << endl;
	return 0;
}
